#include "multigetprofileresponse.h"


#include <QDateTime>
#include <QDebug>


MultiGetProfileResponse::MultiGetProfileResponse(QObject *parent) :
    Network::JSONResponse(parent)
{
}


QList<UserProfile*> MultiGetProfileResponse::getListUserProfile()const
{
    return m_listUserProfile;
}


void MultiGetProfileResponse::parse(const QVariant &data)
{
    // qDebug() << "Get profile of friend:";
    // qDebug() << data;
    if (data.type() != QVariant::Map){
        setError(Response::Property, "The field of \"map\" isn't a map");
        return;
    }

    QVariantMap map = data.toMap();

    if(!map.contains("list")){
        setError(Response::Property, "The field of \"list\" is missing");
        return;
    }

    QVariant listVariant = map.value("list");
    if(listVariant.type() != QVariant::List) {
        setError(Response::Property, "The value of the field of \"list\" is incompatible");
        return;
    }

    QVariantList list = listVariant.toList();
    bool ok;
    foreach (QVariant item, list) {
        if (item.type() != QVariant::Map) {
            setError(Network::Response::Property, QString("The field of \"map\" isn't a map"));
            return;
        }



        QVariantMap variantMap = item.toMap();

        // Parse uid
        int32_t userId = 0;
        {
            if (!variantMap.contains("uid")) {
                setError(Response::Property, "The field of \"uid\" is missing");
                return;
            }
            QVariant userIdVariant = variantMap.value("uid");
            userId = userIdVariant.toInt(&ok);
        }
        if (!ok)
            return;

        // Parse userName
        QString userName("");
        {
            if (!variantMap.contains("urn")) {
                setError(Response::Property, "The field of \"urn\" is missing");
                return;
            }
            QVariant userNameVariant = variantMap.value("urn");
            if (userNameVariant.type() != QVariant::String) {
                setError(Response::Property, "The value of the field of \"urn\" is incompatible");
                return;
            }
            userName = userNameVariant.toString();
        }
        // Parse displayName
        if (!variantMap.contains("dpn")) {
            setError(Response::Property, "The field of \"dpn\" is missing");
            return;
        }

        QVariant displayNameVariant = variantMap.value("dpn");
        if (displayNameVariant.type() != QVariant::String) {
            setError(Response::Property, "The value of the field of \"dpn\" isn incompatible");
            return;
        }

        QString displayName = displayNameVariant.toString();


        // Parse email
        if (!variantMap.contains("eml")) {
            setError(Response::Property, "The field of \"eml\" is missing");
            return;
        }

        QVariant emailVariant = variantMap.value("eml");
        if (emailVariant.type() != QVariant::String) {
            setError(Response::Property, "The value of the field of \"eml\" is incompatible");
            return;
        }

        QString email = emailVariant.toString();

        // Parse profileUrl
        if (!variantMap.contains("url")) {
            setError(Response::Property, "The field of \"url\" is missing");
            return;
        }

        QVariant profileUrlVariant = variantMap.value("url");
        if (profileUrlVariant.type() != QVariant::String) {
            setError(Response::Property, "The value of the field of \"url\" isn incompatible");
            return;
        }

        QString profileUrl = profileUrlVariant.toString();


        // Parse gender
        if (!variantMap.contains("ged")) {
            setError(Response::Property, "The field of \"ged\" is missing");
            return;
        }

        QVariant genderVariant = variantMap.value("ged");
        int gender = genderVariant.toInt(&ok);
        if (!ok)
            return;


        // Parse birthday
        if (!variantMap.contains("dob")) {
            setError(Response::Property, "The field of \"dob\" is missing");
            return;
        }

        QVariant birthdayVariant = variantMap.value("dob");
        if (birthdayVariant.type() != QVariant::String) {
            setError(Response::Property, "The value of the field of \"dob\" is incompatible");
            return;
        }

        QString birthday = QDateTime(birthdayVariant.toDateTime()).toString("yyyy-MM-dd");
        UserProfile* uProfile = new UserProfile(userId,userName,displayName, profileUrl);
        m_listUserProfile.push_back(uProfile);
    }
}
